package com.easy.service.impl;

import com.easy.annotation.LogMethod;
import com.easy.bean.Train;
import com.easy.bean.TrainRoute;
import com.easy.mapper.TrainRouteMapper;
import com.easy.service.TrainRouteService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class TrainRouteServiceImpl implements TrainRouteService {

    @Autowired
    TrainRouteMapper trainRouteMapper;


    @Override
    @LogMethod(serviceName = "添加路线", recordParam = true, recordReturn = true)
    public TrainRoute add(TrainRoute trainRoute) {
        trainRouteMapper.add(trainRoute);
        TrainRoute result = trainRouteMapper.one(trainRoute.getId());
        return result;
    }

    @Override
    @LogMethod(serviceName = "删除路线", recordParam = true, recordReturn = true)
    @CacheEvict(value = "trainRoute", key = "#p0" ,allEntries = true)//清空value制定的所有实例
    public int del(Integer id) {
        return  trainRouteMapper.del(id);
    }

    @Override
    @LogMethod(serviceName = "编辑路线", recordParam = true, recordReturn = true)
    @CachePut(value = "trainRoute", key = "#p0.id")
    public TrainRoute edit(TrainRoute trainRoute) {
        trainRouteMapper.edit(trainRoute);
        TrainRoute result = trainRouteMapper.one(trainRoute.getId());
        return result;
    }

    @Override
    @Cacheable(value = "trainRoute", key = "#p0")//也可以#p0表示第一个参数
    public TrainRoute one(Integer id) {
        return trainRouteMapper.one(id);
    }

    @Override
    public void page(Page page, TrainRoute trainRoute) {
        int count = trainRouteMapper.pageCount(trainRoute);
        Object data = trainRouteMapper.page(page,trainRoute);
        page.setCount(count);
        page.setData(data);
    }
}
